package com.motherchild.user.controller;

import com.motherchild.common.ApiResponse;
import com.motherchild.user.dto.CartItemDTO;
import com.motherchild.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    public ApiResponse<List<CartItemDTO>> getCartItems(@RequestParam Long userId) {
        List<CartItemDTO> cartItems = cartService.getCartItems(userId);
        return ApiResponse.success(cartItems);
    }

    @PutMapping("/add")
    public ApiResponse<Void> addToCart(@RequestParam Long productId, Long count, Integer userId) {
        CartItemDTO cartItemDTO = new CartItemDTO();
        cartItemDTO.setProductId(productId);
        cartItemDTO.setCount(count);
        cartItemDTO.setUserId(userId);
        cartService.addToCart(cartItemDTO);
        return ApiResponse.success();
    }

    @DeleteMapping("/delete")
    public ApiResponse<Void> removeFromCart(@RequestParam Long productId, Long userId) {
        cartService.removeFromCart(productId, userId);
        return ApiResponse.success();
    }

    @PutMapping("/update")
    public ApiResponse<Void> updateCartItem(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer count) {
        cartService.updateCartItem(userId, productId, count);
        return ApiResponse.success();
    }
} 